<template>
  <div class="user-info-container">
    <h2>我的信息</h2>
    
    <div class="info-card">
      <div class="avatar-section">
        <img 
          :src="user.avatarUrl || '/src/images/default-avatar.jpg'"
          alt="用户头像"
          class="avatar-image">
      </div>
      
      <div class="info-section">
        <div class="info-item">
          <span class="label">用户名：</span>
          <span class="value">{{ user.userName }}</span>
        </div>
        
        <div class="info-item">
          <span class="label">手机号：</span>
          <span class="value">{{ user.phone }}</span>
        </div>
      </div>
      
      <button 
        @click="goToEditProfile"
        class="edit-btn">
        修改我的信息
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

const user = ref({
  userName: '测试用户',
  phone: '13812345678',
  avatarUrl: '/src/images/demo.jpg'
});

// 在组件挂载时从localStorage获取用户信息
onMounted(() => {
  try {
    const userInfoStr = localStorage.getItem('userInfo');
    if (userInfoStr) {
      const userInfo = JSON.parse(userInfoStr);
      user.value = {
        ...user.value,
        ...userInfo
      };
    }
  } catch (error) {
    console.error('获取用户信息失败:', error);
  }
});

const goToEditProfile = () => {
  router.push('/user/profile');
};
</script>

<style scoped>
.user-info-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.info-card {
  background-color: #f9f9f9;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  text-align: center;
}

.avatar-section {
  margin-bottom: 20px;
}

.avatar-image {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #ddd;
}

.info-section {
  margin-bottom: 25px;
}

.info-item {
  margin-bottom: 15px;
  font-size: 16px;
}

.label {
  font-weight: bold;
  color: #333;
}

.value {
  color: #666;
}

.edit-btn {
  background-color: #4CAF50;
  color: white;
  padding: 12px 24px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

.edit-btn:hover {
  background-color: #45a049;
}
</style>